package com.donger.upms.biz.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.donger.common.core.utils.SpringContextUtil;
import com.donger.upms.api.entity.SysDictItem;
import com.donger.upms.biz.service.impl.SysDictItemServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典工具类
 * TODO 修改常用字典存储到缓存中
 *
 * @author xyx
 * @date 2019-01-20 19:39
 */
public class DictUtil {

    public static final String CACHE_DICT_MAP = "dictMap";
    private static final SysDictItemServiceImpl sysDictItemService = SpringContextUtil.getBean("sysDictItemService", SysDictItemServiceImpl.class);
    private static Map<String, List<SysDictItem>> DICT_MAP;

    static {
        DICT_MAP = sysDictItemService.list().stream().collect(Collectors.groupingBy(SysDictItem::getType));
    }

    private DictUtil() {
    }

    public static List<SysDictItem> getDictList(String type) {
        if (CollectionUtil.isEmpty(DICT_MAP)) {
            DICT_MAP = sysDictItemService.list().stream().collect(Collectors.groupingBy(SysDictItem::getType));
        }
        return DICT_MAP.get(type);
    }

}
